var tsc = require('../util/ble/tsc.js')
var app = getApp();

var jpPrinterObj = {

	createNew: function() {
		
		var looptime = 0;
		var currentTime = 1;
		var lastData = 0;
		var oneTimeData = 0;
		var buffSize = [];
		var buffIndex = 0;
		var printNum = [];
		var printNumIndex = 0;
		var printerNum = 1;
		var currentPrint = 1;
		var isReceiptSend = false;
		var isLabelSend = false;
		let list = []
		let numList = []
		let j = 0
		for (let i = 20; i < 200; i += 10) {
			list[j] = i;
			j++
		}
		for (let i = 1; i < 10; i++) {
			numList[i - 1] = i
		}
		this.buffSize = list;
		this.oneTimeData = list[0];
		this.printNum = numList;
		this.printerNum = numList[0];
		let {
			BLEInformation
		} = app.globalData;
		jpPrinterObj.printData = function(){
			var command = tsc.jpPrinter.createNew()
			command.setText(130, 30, "TSS24.BF2", 1, 1, `{item.proId}{item.proColor}{item.proSize}`)
			command.setPagePrint()
			this.prepareSend(command.getData())
		}

		//准备发送，根据每次发送字节数来处理分包数量1
		jpPrinterObj.prepareSend = function(buff) {
			let that = this
			let time = that.oneTimeData
			let looptime = parseInt(buff.length / time);
			let lastData = parseInt(buff.length % time);
			this.looptime = looptime + 1;
			this.lastData = lastData;
			this.currentTime = 1;
			that.Send(buff)
		}
		
		//分包发送
		jpPrinterObj.Send = function(buff) {
			let that = this
			let {
				currentTime,
				looptime: loopTime,
				lastData,
				oneTimeData: onTimeData,
				printerNum: printNum,
				currentPrint
			} = that;
			let buf;
			let dataView;
			if (currentTime < loopTime) {
				buf = new ArrayBuffer(onTimeData)
				dataView = new DataView(buf)
				for (var i = 0; i < onTimeData; ++i) {
					dataView.setUint8(i, buff[(currentTime - 1) * onTimeData + i])
				}
			} else {
				buf = new ArrayBuffer(lastData)
				dataView = new DataView(buf)
				for (var i = 0; i < lastData; ++i) {
					dataView.setUint8(i, buff[(currentTime - 1) * onTimeData + i])
				}
			}
			console.log("第" + currentTime + "次发送数据大小为：" + buf.byteLength)
			let {
				BLEInformation
			} = app.globalData
			// console.log(BLEInformation)
			plus.bluetooth.writeBLECharacteristicValue({
				deviceId: BLEInformation.deviceId,
				serviceId: BLEInformation.writeServiceId,
				characteristicId: BLEInformation.writeCharaterId,
				value: buf,
				success: function(res) {
					setTimeout(() => {
						currentTime++
						if (currentTime <= loopTime) {
							that.currentTime = currentTime;
							that.Send(buff)
						} else {
							
							uni.showToast({
								title: '已打印第' + currentPrint + '张',
							})
							if (currentPrint == printNum) {
								that.looptime = 0;
								that.lastData = 0;
								that.currentTime = 1;
								that.isReceiptSend = false;
								that.isLabelSend = false;
								that.currentPrint = 1;
							} else {
								currentPrint++;
								that.currentPrint = currentPrint;
								that.currentTime = 1;
								that.Send(buff)
							}
						}
					}, 10); //这里需要加延时，具体原因不清楚
				},
				fail: function(e) {
					console.log(e)
				},
				complete: function() {
		
				}
			})
		}
				


		return jpPrinterObj;
	},

};
module.exports.jpPrinterObj = jpPrinterObj;
